﻿using System;
using System.Globalization;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Media;

namespace Gmantis.Controls.Chart
{
    public class ThemeConverter : IValueConverter
    {
        // Fields
        private static Brush DefaultForeground = new SolidColorBrush(Colors.Black);

        // Methods
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            ResourceDictionary dictionary = value as ResourceDictionary;
            object[] objArray = parameter as object[];
            if ((objArray == null) || (objArray.Length < 2))
            {
                return null;
            }
            string key = objArray[0] as string;
            if ((value != null) && (key != null))
            {
                if (dictionary.Contains(key))
                {
                    value = dictionary[key];
                }
                else
                {
                    if (key.EndsWith("Brush"))
                    {
                        return null;
                    }
                    if ((key.EndsWith("Thickness") || key.EndsWith("Padding")) || key.EndsWith("Margin"))
                    {
                        return new Thickness();
                    }
                    if (key.EndsWith("CornerRadius"))
                    {
                        return new CornerRadius();
                    }
                }
                string s = value as string;
                if (s == null)
                {
                    return value;
                }
                if (key.EndsWith("CornerRadius"))
                {
                    return ParseCornerRadius(s);
                }
                if ((!key.EndsWith("Thickness") && !key.EndsWith("Padding")) && !key.EndsWith("Margin"))
                {
                    return value;
                }
                return ParseThickness(s);
            }
            if (key != null)
            {
                if (key.EndsWith("Foreground_Brush"))
                {
                    return FindParentForeground(objArray[1] as FrameworkElement);
                }
                if (key.EndsWith("Brush"))
                {
                    return null;
                }
                if ((key.EndsWith("Thickness") || key.EndsWith("Padding")) || key.EndsWith("Margin"))
                {
                    return new Thickness();
                }
                if (key.EndsWith("CornerRadius"))
                {
                    return new CornerRadius();
                }
            }
            return value;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }

        private static Brush FindParentForeground(FrameworkElement fe)
        {
            Brush foreground = null;
            while (fe != null)
            {
                DependencyObject parent = VisualTreeHelper.GetParent(fe);
                Control control = parent as Control;
                if (control != null)
                {
                    foreground = control.Foreground;
                    break;
                }
                fe = parent as FrameworkElement;
            }
            if (foreground == null)
            {
                return DefaultForeground;
            }
            return foreground;
        }

        private static CornerRadius ParseCornerRadius(string s)
        {
            CornerRadius radius = new CornerRadius();
            if (string.IsNullOrEmpty(s))
            {
                return radius;
            }
            string[] strArray = s.Split(new char[] { ',', ' ' });
            if (strArray.Length == 1)
            {
                return new CornerRadius(double.Parse(strArray[0], CultureInfo.InvariantCulture));
            }
            if (strArray.Length != 4)
            {
                throw new FormatException(string.Format("String {0} does not represent CornerRadius", s));
            }
            return new CornerRadius(double.Parse(strArray[0], CultureInfo.InvariantCulture), double.Parse(strArray[1], CultureInfo.InvariantCulture), double.Parse(strArray[2], CultureInfo.InvariantCulture), double.Parse(strArray[3], CultureInfo.InvariantCulture));
        }

        private static Thickness ParseThickness(string s)
        {
            Thickness thickness = new Thickness();
            if (string.IsNullOrEmpty(s))
            {
                return thickness;
            }
            string[] strArray = s.Split(new char[] { ',', ' ' });
            if (strArray.Length == 1)
            {
                return new Thickness(double.Parse(strArray[0], CultureInfo.InvariantCulture));
            }
            if (strArray.Length != 4)
            {
                throw new FormatException(string.Format("String {0} does not represent Thickness", s));
            }
            return new Thickness(double.Parse(strArray[0], CultureInfo.InvariantCulture), double.Parse(strArray[1], CultureInfo.InvariantCulture), double.Parse(strArray[2], CultureInfo.InvariantCulture), double.Parse(strArray[3], CultureInfo.InvariantCulture));
        }
    }
}
